﻿using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.Activity.Model;
using Shop.Repository.LinqHelper;
using SqlSugar;
using WeDonekRpc.Client;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class ActivityRepository : ShopBaseRepository<DBShopActivity, long>, IActivityRepository
    {
        public ActivityRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public long Add (ActivityAdd data)
        {
            DBShopActivity add = data.ConvertMap<ActivityAdd, DBShopActivity>();
            add.Id = IdentityHelper.CreateId();
            add.AddTime = DateTime.Now;
            add.ActivityStatus = ActivityStatus.起草;
            base.Insert(add);
            return add.Id;
        }
        public ValidActivityDto[] GetValidActivity (DateTime begin, DateTime end, long skuId)
        {
            ISugarQueryable<ValidActivityDto>[] tables = new ISugarQueryable<ValidActivityDto>[]
            {
                base.Queryable.Where(c => c.ActivityType == ActivityType.店铺促销 &&
                c.Range == DiscountRangeType.全场 &&
                c.ActivityStatus == ActivityStatus.已审核 &&
                c.BeginTime <= end &&
                c.EndTime >= begin).Select(c=>new ValidActivityDto
                {
                    Id = c.Id,
                    ActivityTitle = c.ActivityTitle,
                    Range=c.Range,
                    ActivityType = c.ActivityType,
                }),
                base.Queryable.Where((c)=>
                c.ActivityType == ActivityType.店铺促销 &&
                 c.Range == DiscountRangeType.部分不参与 &&
                c.ActivityStatus == ActivityStatus.已审核 &&
                c.BeginTime <= end &&
                c.EndTime >= begin &&
                SqlFunc.Subqueryable<DBShopActivityGoods>().Where(a=>a.ActivityId==c.Id && a.SkuId==skuId).NotAny()).Select(c=>new ValidActivityDto
                {
                    Id = c.Id,
                    ActivityTitle = c.ActivityTitle,
                    Range=c.Range,
                    ActivityType = c.ActivityType
                }),
                base.Queryable.InnerJoin<DBShopActivityGoods>((c,b)=>c.Id==b.ActivityId &&
                b.SkuId == skuId &&
                c.ActivityStatus == ActivityStatus.已审核 &&
                c.BeginTime <= end &&
                c.EndTime >= begin).Select((c,a)=>new ValidActivityDto
                {
                    Id = c.Id,
                    ActivityTitle = c.ActivityTitle,
                    Range=c.Range,
                    ActivityType = c.ActivityType,
                    ActivityPrice=a.ActivityPrice
                }),
            };
            return base.Gets<ValidActivityDto>(tables);
        }

        public Result[] Query<Result> (ActivityQueryParam query, IBasicPage paging, out int count) where Result : class
        {
            paging.InitOrderBy("Id", true);
            return base.Query<Result>(query.ToWhere(), paging, out count);
        }
    }
}
